package com.closer.server.mapper.problem;

import com.closer.server.biz.problem.dto.JudgeConfig;
import com.closer.server.biz.problem.dto.SearchRequest;
import com.closer.server.biz.problem.dto.vo.AdminProblemVO;
import com.closer.server.biz.problem.dto.vo.ProblemVO;
import com.closer.server.biz.problem.dto.SearchByAdminRequest;
import com.closer.server.support.sql.QueryWrapperHelper;
import com.mybatisflex.core.BaseMapper;
import com.closer.server.biz.problem.entity.Problem;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;

/**
 * 题目表 映射层。
 *
 * @author closer
 */
public interface ProblemMapper extends BaseMapper<Problem> {

    /**
     * 获取题目列表
     * @param params 查询参数
     * @return 用户列表
     */
    default Page<ProblemVO> getProblemVOList(SearchRequest params) {
        var queryWrapper = QueryWrapper.create()
                .select("problem.id","problem.title", "problem.description", "problem.difficulty",
                        "problem.judge_config", "problem.input_description", "problem.output_description",
                        "problem.sample_input", "problem.sample_output", "problem.hint", "problem.submission_count", "problem.accepted_count")
                .from("problem")
                .orderBy("problem.create_time asc");
        QueryWrapperHelper.likeIfPresent(queryWrapper, params.getTitle(), "problem.title");
        QueryWrapperHelper.eqIfPresent(queryWrapper, params.getDifficulty(), "problem.difficulty");
        return paginateAs(params.getPageNumber(), params.getPageSize(),queryWrapper, ProblemVO.class);
    }

    /**
     * 获取题目列表【管理员】
     * @param params 搜索参数
     * @return 用户列表
     */
    default Page<AdminProblemVO> getProblemVOList(SearchByAdminRequest params) {
        var queryWrapper = QueryWrapper.create()
                .select("problem.*")
                .from("problem")
                .orderBy("problem.create_time desc");
        QueryWrapperHelper.likeIfPresent(queryWrapper, params.getTitle(), "problem.title");
        QueryWrapperHelper.eqIfPresent(queryWrapper, params.getCreatedUserId(), "problem.created_user_id");
        QueryWrapperHelper.eqIfPresent(queryWrapper, params.getDifficulty(), "problem.difficulty");
        QueryWrapperHelper.eqIfPresent(queryWrapper, params.getVisible(), "problem.visible");
        return paginateAs(params.getPageNumber(), params.getPageSize(),queryWrapper, AdminProblemVO.class);
    }

    /**
     * 增加题目提交数
     * @param problemId 题目ID
     * @return 是否成功
     */
    default boolean incrementSubmissionCount(Long problemId){
        return UpdateChain.of(Problem.class)
                .setRaw(Problem::getSubmissionCount, "submission_count + 1")
                .where(Problem::getId).eq(problemId)
                .update();
    }

    /**
     * 获取题目判题配置
     * @param problemId 题目ID
     * @return 判题配置
     */
    default String getJudgeConfig(Long problemId){
        var queryWrapper = QueryWrapper.create()
                .select("problem.judge_config")
                .from("problem")
                .eq(Problem::getId, problemId);
        return selectOneByQuery(queryWrapper).getJudgeConfig();
    }

    default ProblemVO getProblemVO(Long problemId){
        var queryWrapper = QueryWrapper.create()
                .select("problem.id","problem.title", "problem.description", "problem.difficulty",
                        "problem.judge_config", "problem.input_description", "problem.output_description",
                        "problem.sample_input", "problem.sample_output", "problem.hint", "problem.submission_count", "problem.accepted_count")
                .from("problem")
                .eq(Problem::getId, problemId);
        return selectOneByQueryAs(queryWrapper, ProblemVO.class);
     }
}
